package toy.keli.picmap.service;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import static android.content.Context.BIND_AUTO_CREATE;

public class Connection implements ServiceConnection {
    private String methodName;
    private Object[] params;
    private Bind mBind;
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        Bind mBinder = (Bind) service;
        Method method = null;
        try {
            method = mBinder.getClass().getMethod(methodName);
            method.invoke(mBinder,params);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {

    }

    public void run(Context ct,String method, Object[] args) {
        Intent bindIntent = new Intent(ct,FindPicService.class);
        ct.bindService(bindIntent, this, BIND_AUTO_CREATE);
        methodName=method;
        params=args;
    }

    public static class Bind extends Binder {
        public void doTask() {
            Log.d("Info", "doTask");
        }
    }
}
